package com.sense.kukoo.biz_main.vm

import android.util.Log
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.mvvm.vm.BaseViewModel
import com.sense.kukoo.common.push.FcmManager
import com.sense.kukoo.common.repo.Repo

class MainViewModel: BaseViewModel() {

    private val TAG: String = javaClass.simpleName

    override fun createModel(): BaseMvvmModel? {
        FcmManager.setListener(tokenListener)
        return null
    }

    override fun initData() {}

    fun registerPush(){
        FcmManager.getToken()?.let{
            registerPush(it)
        }
    }

    fun registerPush(token: String){
        if (!Repo.login.isLogin()) {
            return
        }
        request{
            val result = Repo.http.registerPush(token)
            if (result.isSuccess() && true == result.data) {
                //
            } else {
                Log.e(TAG, "result=" + result.msg)
            }
        }
    }

    override fun onCleared() {
        FcmManager.setListener(null)
        super.onCleared()
    }

    private val tokenListener: (String)->Unit = { token ->
        registerPush(token)
    }

}